package com.paperyy.module.controller;

import com.paperyy.module.bean.Consume;
import com.paperyy.module.bean.User;
import com.paperyy.module.bean.Vip;
import com.paperyy.module.dao.ConsumeDao;
import com.paperyy.module.dao.UserDao;

import com.paperyy.module.service.impl.UserServiceImpl;
import com.paperyy.module.service.impl.VipServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/vip")
public class VipController {

    @Autowired
    private VipServiceImpl vipService;
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private ConsumeDao consumeDao;
    @Autowired
    private UserDao userDao;


    @RequestMapping(value = "/index")
    public String index(Model model, Integer uid) {


        User user = userService.findUser(uid);
        //根据uid查找到余额
        BigDecimal umoney = user.getMoney();
        model.addAttribute("umoney", umoney);
        //查询所有套餐
        List<Vip> vipList = vipService.findAll();
        model.addAttribute("list", vipList);

        return "vip";
    }

    @RequestMapping(value = "/update")
    public String update(Model model, Integer uid, Integer vid) {

//根据vid获取具体套餐
        Vip vip = vipService.findVip(vid);

        //判断null套餐
        if (vip == null) {
            model.addAttribute("ull", "错误");
            return "vip";
        }
        //获取套餐的时间
        Integer atime = vip.getAtime();
        //获取套餐的价格
        BigDecimal vmoney = vip.getVmoney();
        //具体的用户
        User user = userService.findUser(uid);
        //用户套餐时间
        Date time = user.getTime();
        //获取用户余额
        BigDecimal money = user.getMoney();

        if (money.compareTo(vmoney) == -1) {
            //用于前端弹窗
            model.addAttribute("age", "1");
            //用户余额
            model.addAttribute("umoney", money);
            return "vip";
        }
        //设置一个日历
        Calendar ca = Calendar.getInstance();
        //获取用户是否是会员
        Integer userV = user.getV();
        if (userV == 0) {
            //不是会员，将今天的时间添加到日历中
            Date tiem1 = new Date();
            ca.setTime(tiem1);
        } else {
            //是会员，将会员到期时间添加到日历
            ca.setTime(time);
        }
        //将套餐时间加上
        ca.add(Calendar.DATE, atime);
        //得到新的到期时间
        time = ca.getTime();

        //根据uid更新用户信息表
        user.setId(uid);
        user.setV(1);
        user.setMoney(money.subtract(vmoney));
        user.setTime(time);
        userDao.updateById(user);

//充值完成后的用户余额
        model.addAttribute("umoney", user.getMoney());

        List<Vip> vipList = vipService.findAll();
        model.addAttribute("list", vipList);

        //向消费记录表中插入数据集
        Consume consume = new Consume();
        consume.setCmoney(vmoney);
        consume.setCtime(new Date());
        String str = "用户开通了" + atime + "天的会员";
        consume.setDetails(str);
        consume.setName(user.getName());
        consumeDao.insert(consume);

        return "vip";

    }
}
